package artid.com.arttogo.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;

import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.CollecterActivity;
import artid.com.arttogo.activity.OrganizationActivity;
import artid.com.arttogo.bean.CollectionList;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.BitmapCache;
import artid.com.arttogo.utils.VeDate;

public class PersonCollectionAdapter extends BaseAdapter {
	private Context mContext;
	LayoutInflater mInflater;
	ViewHolder holder;
	public List<CollectionList> mList;
	private ImageLoader mImageLoader;

	public PersonCollectionAdapter(Context mContext, List<CollectionList> bean) {
		this.mContext = mContext;
		mInflater = LayoutInflater.from(mContext);
		this.mList = bean;
		RequestQueue mQueue = Volley.newRequestQueue(mContext);
		mImageLoader = new ImageLoader(mQueue, new BitmapCache());
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public void addData(List<CollectionList> list) {
		mList.addAll(list);
		notifyDataSetChanged();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			vh = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_fragment_collection,
					null);
			vh.name = (TextView) convertView
					.findViewById(R.id.item_attention_name);
			vh.detail = (TextView) convertView
					.findViewById(R.id.item_collect_msg);
			vh.time = (TextView) convertView
					.findViewById(R.id.item_attention_message);
			vh.artname = (TextView) convertView
					.findViewById(R.id.item_collection_artname);
			// .findViewById(R.id.text_attention_share);
			vh.auth = (ImageView) convertView
					.findViewById(R.id.item_collection_collerter);
			vh.icon = (ImageView) convertView
					.findViewById(R.id.item_attention_photo);
			vh.pic = (ImageView) convertView
					.findViewById(R.id.item_collection_photo);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		final CollectionList itemFollow = mList.get(position);
		vh.name.setText(itemFollow.getRealname());
		if (itemFollow.getIntroduction().equals("暂无")|| TextUtils.isEmpty(itemFollow.getIntroduction())) {
			vh.detail.setText("描述信息暂无");
		}else {
			vh.detail.setText(itemFollow.getIntroduction());
		}
	
		
		vh.time.setText(VeDate.getStrMDTime(itemFollow.getTime()));
		vh.artname.setText(itemFollow.getArtname());
		if (itemFollow.getIs_auth().equals("2")) {
			vh.auth.setVisibility(View.VISIBLE);
		}else {
			vh.auth.setVisibility(View.GONE);
		}

		if (itemFollow.getArtimg().contains("http")) {
			ImageListener listener = ImageLoader.getImageListener(vh.icon,
					R.drawable.ic_menu_rotate, R.drawable.ic_menu_rotate);
			mImageLoader.get(itemFollow.getArtimg(), listener);
		}
		if (itemFollow.getImgurl().contains("http")) {

			Glide.with(mContext)
					.load(itemFollow.getImgurl() + AppUtil.picWidthUrl)
					.placeholder(R.drawable.ic_menu_rotate)
					.error(R.drawable.ic_menu_rotate).into(vh.pic);

		}
		vh.icon.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (TextUtils.isEmpty(itemFollow.getArtist_id())) {
					return;
				}
				if (itemFollow.getIs_type().equals("1")) {
					Intent intent = new Intent(mContext,CollecterActivity.class);
					intent.putExtra("id", itemFollow.getArtist_id());
					mContext.startActivity(intent);
				}else if (itemFollow.getIs_type().equals("2")) {
					Intent intent = new Intent(mContext,OrganizationActivity.class);
					intent.putExtra("id", itemFollow.getArtist_id());
					mContext.startActivity(intent);
				}
			
			}
		});

		// vh.share.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// // TODO Auto-generated method stub
		// // AppUtil.showShare(mContext, itemFollow.getArttitle(),
		// itemFollow.getArtcontent(), itemFollow.getImgurl(),
		// itemFollow.getImgurl());
		// }
		// });
		return convertView;
	}

	class ViewHolder {

		TextView name, detail, time, zan, comment, share, artname;
		ImageView icon, pic, auth;

	}
}
